package com.skywolf.chem.match.controller;


import com.alibaba.fastjson.JSON;
import com.skywolf.chem.common.sysconfig.exception.BizException;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.match.entity.Info;
import com.skywolf.chem.match.params.page.MatchInfoPageParam;
import com.skywolf.chem.match.params.param.ApplyUserInfoParam;
import com.skywolf.chem.match.params.vo.MatchInfoQueryVo;
import com.skywolf.chem.match.params.vo.MatchPageInfoQueryVo;
import com.skywolf.chem.match.service.IMatchInfoService;
import com.skywolf.chem.match.service.IUserCollectRecordService;
import com.skywolf.chem.match.utils.FastJsonUtils;
import com.skywolf.chem.match.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * <p>
 * 赛事详情 前端控制器
 * </p>
 *
 * @author sj
 * @since 2021-09-24
 */
@AllArgsConstructor
@RestController
@RequestMapping("/match/info")
@Api(value = "赛事：活动管理", tags = {"赛事：活动管理API"})
public class MatchInfoController extends BaseController {

    private final IMatchInfoService iMatchInfoService;
    private final IUserCollectRecordService collectRecordService;

    @ApiOperation(value = "分页获取活动列表", notes = "分页获取活动列表")
    @PostMapping(value = "/get/query/list")
    public RespData<PageVO<MatchPageInfoQueryVo>> getMatchQueryList(@RequestBody @Validated MatchInfoPageParam pageParam) throws Exception {
        pageParam.setUserId(getUserId());
        PageVO<MatchPageInfoQueryVo> pageVo = iMatchInfoService.getMatchQueryList(pageParam);
        return RespData.success(pageVo);
    }

    @ApiOperation(value = "根据活动编号查询活动详情", notes = "根据活动编号查询活动详情")
    @GetMapping(value = "/get/by/{id}")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "path", dataType = "Long")
    public RespData<MatchInfoQueryVo> getMatchInfoById(@PathVariable("id") Long id) throws Exception {
        MatchInfoQueryVo vo = iMatchInfoService.getMatchInfoById(id, getUserId());
        return RespData.success(vo);
    }


    @ApiOperation(value = "收藏/取消收藏活动", notes = "收藏/取消收藏活动")
    @GetMapping(value = "/collect/by/{id}")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "path", dataType = "Long")
    public RespData collectMatch(@PathVariable("id") Long id) throws Exception {
        collectRecordService.collectMatch(getUserId(), id);
        return RespData.success();
    }

    @ApiOperation(value = "根据活动编号获取用户自定义标签", notes = "根据活动编号获取用户自定义标签")
    @GetMapping(value = "/get/tag/{id}")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "path", dataType = "Long")
    public RespData<ApplyUserInfoParam.ApplyUserCustomTagModel> getTagByMatchId(@PathVariable("id") Long id) {
        Info info = iMatchInfoService.getById(id);
        if (null == info) {
            throw new BizException("当前活动不存在！");
        }
        return RespData.success(StringUtils.isNotBlank(info.getApplyUserCustomTag())||"[]".equals(info.getApplyUserCustomTag())
                ? FastJsonUtils.toList(info.getApplyUserCustomTag(), ApplyUserInfoParam.ApplyUserCustomTagModel.class) : new ArrayList<>());
    }



}
